package com.youkeyi.ddy.cloud.basic.crm.customer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.customer.entity.SysCustomer;
import com.youkeyi.ddy.cloud.basic.crm.customer.service.SysCustomerService;
import com.youkeyi.ddy.cloud.basic.crm.source.entity.SysCustomerSource;
import com.youkeyi.ddy.cloud.basic.crm.source.service.SysCustomerSourceService;
import com.youkeyi.ddy.cloud.basic.crm.type.entity.SysCustomerType;
import com.youkeyi.ddy.cloud.basic.crm.type.service.SysCustomerTypeService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.CrmCustomerCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicCrmConstant;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-01
 */
@Service
public class SysCustomerController implements CrmCustomerCloudService {

    @Autowired
    private SysCustomerService sysCustomerService;
    @Autowired
    private SysCustomerSourceService sysCustomerSourceService;
    @Autowired
    private SysCustomerTypeService sysCustomerTypeService;

    @Override
    public CrmCustomerListCloudDto query(CrmCustomerQueryCloudParam param) {
        CrmCustomerListCloudDto dto = new CrmCustomerListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerPrimaryKeys())) {
            param.getCustomerPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getPublicCorpKeys())) {
            param.getPublicCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getPublicUserKeys())) {
            param.getPublicUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCsCorpKeys())) {
            param.getCsCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCsUserKeys())) {
            param.getCsUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getAdviserCorpKeys())) {
            param.getAdviserCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getAdviserUserKeys())) {
            param.getAdviserUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getSourceCorpKeys())) {
            param.getSourceCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getSourceUserKeys())) {
            param.getSourceUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getSourceChannels())) {
            param.getSourceChannels().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getServerCorpKeys())) {
            param.getServerCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getServerUserKeys())) {
            param.getServerUserKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerTypeKeys())) {
            param.getCustomerTypeKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomer> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerPrimaryKeys()), SysCustomer::getPrimaryKey, param.getCustomerPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomer::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCsCorpKeys()), SysCustomer::getCsCorpKey, param.getCsCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCsUserKeys()), SysCustomer::getCsUserKey, param.getCsUserKeys())
                .in(CollectionUtils.isNotEmpty(param.getAdviserUserKeys()), SysCustomer::getAdviserUserKey, param.getAdviserUserKeys())
                .in(CollectionUtils.isNotEmpty(param.getAdviserCorpKeys()), SysCustomer::getAdviserCorpKey, param.getAdviserCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getSourceCorpKeys()), SysCustomer::getSourceCorpKey, param.getSourceCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getSourceUserKeys()), SysCustomer::getSourceUserKey, param.getSourceUserKeys())
                .in(CollectionUtils.isNotEmpty(param.getSourceChannels()), SysCustomer::getSourceChannel, param.getSourceChannels())
                .in(CollectionUtils.isNotEmpty(param.getServerCorpKeys()), SysCustomer::getServerCorpKey, param.getServerCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getServerUserKeys()), SysCustomer::getServerUserKey, param.getServerUserKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerTypeKeys()), SysCustomer::getCustomerTypeKey, param.getCustomerTypeKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysCustomer::getStatus, param.getStatuses())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysCustomer::getName, param.getNameFuzzy())
                .eq(StringUtils.isNotBlank(param.getName()), SysCustomer::getName, param.getName())
                .like(StringUtils.isNotBlank(param.getEnglishNameFuzzy()), SysCustomer::getEnglishName, param.getEnglishNameFuzzy())
                .eq(StringUtils.isNotBlank(param.getEnglishName()), SysCustomer::getEnglishName, param.getEnglishName())
                .like(StringUtils.isNotBlank(param.getPhoneFuzzy()), SysCustomer::getPhone, param.getPhoneFuzzy())
                .eq(StringUtils.isNotBlank(param.getPhone()), SysCustomer::getPhone, param.getPhone())
                .like(StringUtils.isNotBlank(param.getIdCardFuzzy()), SysCustomer::getIdCard, param.getIdCardFuzzy())
                .like(StringUtils.isNotBlank(param.getEmailFuzzy()), SysCustomer::getEmail, param.getEmailFuzzy())
                .like(StringUtils.isNotBlank(param.getCompanyFuzzy()), SysCustomer::getCompany, param.getCompanyFuzzy())
                .like(StringUtils.isNotBlank(param.getLicenseCodeFuzzy()), SysCustomer::getLicenseCode, param.getLicenseCodeFuzzy())
                .between(null != param.getCreateTimeBegin() && null != param.getCreateTimeEnd(), SysCustomer::getCreateTime, param.getCreateTimeBegin(), param.getCreateTimeEnd())
                .between(null != param.getVisaExpirationDateBegin() && null != param.getVisaExpirationDateEnd(), SysCustomer::getVisaExpirationDate, param.getVisaExpirationDateBegin(), param.getVisaExpirationDateEnd())
                .ne(SysCustomer::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;
        if (CollectionUtils.isNotEmpty(param.getPublicCorpKeys())) {
            wrapper.lambda().and( i -> i
                            .in(SysCustomer::getCsCorpKey, param.getPublicCorpKeys())
                            .or().in(SysCustomer::getAdviserCorpKey, param.getPublicCorpKeys())
                            .or().in(SysCustomer::getServerCorpKey, param.getPublicCorpKeys())
                            .or().in(SysCustomer::getSourceCorpKey, param.getPublicCorpKeys())
            );
        }
        if (CollectionUtils.isNotEmpty(param.getPublicUserKeys())) {
            wrapper.lambda().and( i -> i
                    .in(SysCustomer::getCsUserKey, param.getPublicUserKeys())
                    .or().in(SysCustomer::getAdviserUserKey, param.getPublicUserKeys())
                    .or().in(SysCustomer::getServerUserKey, param.getPublicUserKeys())
                    .or().in(SysCustomer::getSourceUserKey, param.getPublicUserKeys())
            );
        }
        // 关键字查询
        if (StringUtils.isNotBlank(param.getKeywords())) {
            wrapper.lambda().and(i ->
                    i.like(SysCustomer::getName, param.getKeywords())
                    .or().like(SysCustomer::getEnglishName, param.getKeywords())
                    .or().like(SysCustomer::getPhone, param.getKeywords())
                    .or().like(SysCustomer::getWechatId, param.getKeywords())
            );
        }

        wrapper.lambda().orderByDesc(SysCustomer::getId);
        Page<SysCustomer> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomer> customerList = sysCustomerService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerList.getRecords())) {
            dto.setTotal(customerList.getTotal());

            QueryWrapper<SysCustomerSource> sourceWrapper = new QueryWrapper<>();
            sourceWrapper.lambda().in(SysCustomerSource::getPrimaryKey, customerList.getRecords().stream().map(SysCustomer::getSourceChannel).collect(Collectors.toList()));
            List<SysCustomerSource> sources = sysCustomerSourceService.list(sourceWrapper);
            Map<String, SysCustomerSource> sourceMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(sources)) {
                sourceMap.putAll(sources.stream().collect(Collectors.toMap(SysCustomerSource::getPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            QueryWrapper<SysCustomerType> typeWrapper = new QueryWrapper<>();
            typeWrapper.lambda().in(SysCustomerType::getPrimaryKey, customerList.getRecords().stream().map(SysCustomer::getCustomerTypeKey).collect(Collectors.toList()));
            List<SysCustomerType> types = sysCustomerTypeService.list(typeWrapper);
            Map<String, String> typeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(types)) {
                typeMap.putAll(types.stream().collect(Collectors.toMap(SysCustomerType::getPrimaryKey, SysCustomerType::getName, (k1, k2) -> k1)));
            }

            List<CrmCustomerListItemCloudDto> items = new ArrayList<>();
            customerList.getRecords().forEach(customer -> {
                CrmCustomerListItemCloudDto item = new CrmCustomerListItemCloudDto();
                item.setCustomerPrimaryKey(customer.getPrimaryKey());
                item.setCorpKey(customer.getCorpKey());
                item.setCsCorpKey(customer.getCsCorpKey());
                item.setCsUserKey(customer.getCsUserKey());
                item.setCsUserName(customer.getCsUserName());
                item.setSourceCorpKey(customer.getSourceCorpKey());
                item.setSourceUserKey(customer.getSourceUserKey());
                item.setSourceUserName(customer.getSourceUserName());
                item.setSourceChannel(customer.getSourceChannel());
                if (sourceMap.containsKey(customer.getSourceChannel())) {
                    SysCustomerSource source = sourceMap.get(customer.getSourceChannel());
                    item.setSourceChannelName(source.getSource());
                    item.setSourceChannelPersonName(source.getSourcePersonName());
                }
                item.setAdviserCorpKey(customer.getAdviserCorpKey());
                item.setAdviserUserKey(customer.getAdviserUserKey());
                item.setAdviserUserName(customer.getAdviserUserName());
                item.setServerCorpKey(customer.getServerCorpKey());
                item.setServerUserKey(customer.getServerUserKey());
                item.setServerUserName(customer.getServerUserName());
                item.setCustomerTypeKey(customer.getCustomerTypeKey());
                item.setCustomerTypeKeyName(typeMap.get(item.getCustomerTypeKey()));
                item.setCode(customer.getCode());
                item.setEnglishName(customer.getEnglishName());
                item.setName(customer.getName());
                item.setWechatId(customer.getWechatId());
                item.setSex(customer.getSex());
                item.setHeight(customer.getHeight());
                item.setWeight(customer.getWeight());
                if (null != customer.getBirthday()) {
                    item.setBirthday(customer.getBirthday().getTime());
                    // 计算年龄
                    Period period = Period.between(SystemDateUtils.long2LocalDateTime(item.getBirthday()).toLocalDate(), LocalDate.now());
                    item.setAge(period.getYears());
                }
                item.setPhone(customer.getPhone());
                item.setIdCard(customer.getIdCard());
                item.setEmail(customer.getEmail());
                item.setAddress(customer.getAddress());
                item.setEducational(customer.getEducational());
                item.setEmergencyContactName(customer.getEmergencyContactName());
                item.setEmergencyContactPhone(customer.getEmergencyContactPhone());
                item.setEmergencyContactRelationship(customer.getEmergencyContactRelationship());
                item.setStatus(customer.getStatus());
                item.setStatusName(YoukeyiDdyCloudBasicCrmConstant.CRM_CUSTOMER_STATUS.getName(item.getStatus()));
                item.setCompany(customer.getCompany());
                item.setJobTitle(customer.getJobTitle());
                item.setIndustry(customer.getIndustry());
                item.setAccountType(customer.getAccountType());
                item.setAccountName(customer.getAccountName());
                item.setBank(customer.getBank());
                item.setBankNo(customer.getBankNo());
                item.setLicenseName(customer.getLicenseName());
                item.setLicenseCode(customer.getLicenseCode());
                item.setIncomeRmb(customer.getIncomeRmb());
                item.setIncomeHkd(customer.getIncomeHkd());
                item.setLeadSource(customer.getLeadSource());
                item.setPotentialDemandKey(customer.getPotentialDemandKey());
                item.setPotentialDemandName(customer.getPotentialDemandName());
                item.setProvince(customer.getProvince());
                item.setCity(customer.getCity());
                item.setAdviserFlag(customer.getAdviserFlag());
                item.setAdviserFlagUserKey(customer.getAdviserFlagUserKey());
                item.setAdviserFlagUserName(customer.getAdviserFlagUserName());
                item.setCsFlag(customer.getCsFlag());
                item.setCsFlagUserKey(customer.getCsFlagUserKey());
                item.setCsFlagUserName(customer.getCsFlagUserName());
                item.setPlace(customer.getPlace());
                item.setInitialFollowTime(customer.getInitialFollowTime() != null ? customer.getInitialFollowTime().getTime() : null);
                item.setInitialFollowDesc(customer.getInitialFollowDesc());
                item.setRemark(customer.getRemark());
                item.setEvaluationDate(customer.getEvaluationDate() != null ? customer.getEvaluationDate().getTime() : null);
                item.setVisaExpirationDate(customer.getVisaExpirationDate() != null ? customer.getVisaExpirationDate().getTime() : null);
                item.setAnnexKey(customer.getAnnexKey());
                item.setIsDeleted(customer.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customer.getCreateTime() != null ? customer.getCreateTime().getTime() : null);
                item.setCreateUserKey(customer.getCreateUserKey());
                item.setCreateUserName(customer.getCreateUserName());
                item.setUpdateTime(customer.getUpdateTime() != null ? customer.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customer.getUpdateUserKey());
                item.setUpdateUserName(customer.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomer customer = new SysCustomer();
        customer.setPrimaryKey(SimpleUUidUtils.getUuid());
        customer.setCorpKey(param.getCorpKey());
        customer.setCsCorpKey(param.getCsCorpKey());
        customer.setCsUserKey(param.getCsUserKey());
        customer.setCsUserName(param.getCsUserName());
        customer.setSourceCorpKey(param.getSourceCorpKey());
        customer.setSourceUserKey(param.getSourceUserKey());
        customer.setSourceUserName(param.getSourceUserName());
        customer.setSourceChannel(param.getSourceChannel());
        customer.setAdviserCorpKey(param.getAdviserCorpKey());
        customer.setAdviserUserKey(param.getAdviserUserKey());
        customer.setAdviserUserName(param.getAdviserUserName());
        customer.setServerCorpKey(param.getServerCorpKey());
        customer.setServerUserKey(param.getServerUserKey());
        customer.setServerUserName(param.getServerUserName());
        customer.setCustomerTypeKey(param.getCustomerTypeKey());
        customer.setCode(StringUtils.isNotBlank(param.getCode()) ? param.getCode() : String.format("CUS-%s", System.currentTimeMillis()));
        customer.setEnglishName(param.getEnglishName());
        customer.setName(param.getName());
        customer.setWechatId(param.getWechatId());
        customer.setSex(param.getSex());
        customer.setHeight(param.getHeight());
        customer.setWeight(param.getWeight());
        customer.setBirthday(param.getBirthday() != null ? new Date(param.getBirthday()) : null);
        customer.setPhone(param.getPhone());
        customer.setIdCard(param.getIdCard());
        customer.setEmail(param.getEmail());
        customer.setAddress(param.getAddress());
        customer.setEducational(param.getEducational());
        customer.setEmergencyContactName(param.getEmergencyContactName());
        customer.setEmergencyContactPhone(param.getEmergencyContactPhone());
        customer.setEmergencyContactRelationship(param.getEmergencyContactRelationship());
        customer.setStatus(param.getStatus());
        customer.setCompany(param.getCompany());
        customer.setJobTitle(param.getJobTitle());
        customer.setIndustry(param.getIndustry());
        customer.setAccountType(param.getAccountType());
        customer.setAccountName(param.getAccountName());
        customer.setBank(param.getBank());
        customer.setBankNo(param.getBankNo());
        customer.setLicenseName(param.getLicenseName());
        customer.setLicenseCode(param.getLicenseCode());
        customer.setIncomeRmb(param.getIncomeRmb());
        customer.setIncomeHkd(param.getIncomeHkd());
        customer.setLeadSource(param.getLeadSource());
        customer.setPotentialDemandKey(param.getPotentialDemandKey());
        customer.setPotentialDemandName(param.getPotentialDemandName());
        customer.setProvince(param.getProvince());
        customer.setCity(param.getCity());
        customer.setAdviserFlag(param.getAdviserFlag());
        customer.setAdviserFlagUserKey(param.getAdviserFlagUserKey());
        customer.setAdviserFlagUserName(param.getAdviserFlagUserName());
        customer.setCsFlag(param.getCsFlag());
        customer.setCsFlagUserKey(param.getCsFlagUserKey());
        customer.setCsFlagUserName(param.getCsFlagUserName());
        customer.setPlace(param.getPlace());
        customer.setInitialFollowTime(param.getInitialFollowTime() != null ? new Date(param.getInitialFollowTime()) : null);
        customer.setInitialFollowDesc(param.getInitialFollowDesc());
        customer.setRemark(param.getRemark());
        customer.setEvaluationDate(param.getEvaluationDate() != null ? new Date(param.getEvaluationDate()) : null);
        customer.setVisaExpirationDate(param.getVisaExpirationDate() != null ? new Date(param.getVisaExpirationDate()) : null);
        customer.setAnnexKey(param.getAnnexKey());
        customer.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customer.setCreateUserKey(param.getCreateUserKey());
        customer.setCreateUserName(param.getCreateUserName());
        customer.setCreateTime(new Date());
        boolean save = sysCustomerService.save(customer);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customer.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomer> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomer::getPrimaryKey, param.getCustomerPrimaryKey());
        SysCustomer customer = new SysCustomer();
        customer.setPrimaryKey(param.getCustomerPrimaryKey());
        customer.setCsCorpKey(param.getCsCorpKey());
        customer.setCsUserKey(param.getCsUserKey());
        customer.setCsUserName(param.getCsUserName());
        customer.setSourceCorpKey(param.getSourceCorpKey());
        customer.setSourceUserKey(param.getSourceUserKey());
        customer.setSourceUserName(param.getSourceUserName());
        customer.setSourceChannel(param.getSourceChannel());
        customer.setAdviserCorpKey(param.getAdviserCorpKey());
        customer.setAdviserUserKey(param.getAdviserUserKey());
        customer.setAdviserUserName(param.getAdviserUserName());
        customer.setServerCorpKey(param.getServerCorpKey());
        customer.setServerUserKey(param.getServerUserKey());
        customer.setServerUserName(param.getServerUserName());
        customer.setCustomerTypeKey(param.getCustomerTypeKey());
        customer.setCode(param.getCode());
        customer.setEnglishName(param.getEnglishName());
        customer.setName(param.getName());
        customer.setWechatId(param.getWechatId());
        customer.setSex(param.getSex());
        customer.setHeight(param.getHeight());
        customer.setWeight(param.getWeight());
        customer.setBirthday(param.getBirthday() != null ? new Date(param.getBirthday()) : null);
        customer.setPhone(param.getPhone());
        customer.setIdCard(param.getIdCard());
        customer.setEmail(param.getEmail());
        customer.setStatus(param.getStatus());
        customer.setAddress(param.getAddress());
        customer.setEducational(param.getEducational());
        customer.setEmergencyContactName(param.getEmergencyContactName());
        customer.setEmergencyContactPhone(param.getEmergencyContactPhone());
        customer.setEmergencyContactRelationship(param.getEmergencyContactRelationship());
        customer.setCompany(param.getCompany());
        customer.setJobTitle(param.getJobTitle());
        customer.setIndustry(param.getIndustry());
        customer.setAccountType(param.getAccountType());
        customer.setAccountName(param.getAccountName());
        customer.setBank(param.getBank());
        customer.setBankNo(param.getBankNo());
        customer.setLicenseName(param.getLicenseName());
        customer.setLicenseCode(param.getLicenseCode());
        customer.setIncomeRmb(param.getIncomeRmb());
        customer.setIncomeHkd(param.getIncomeHkd());
        customer.setLeadSource(param.getLeadSource());
        customer.setPotentialDemandKey(param.getPotentialDemandKey());
        customer.setPotentialDemandName(param.getPotentialDemandName());
        customer.setProvince(param.getProvince());
        customer.setCity(param.getCity());
        customer.setAdviserFlag(param.getAdviserFlag());
        customer.setAdviserFlagUserKey(param.getAdviserFlagUserKey());
        customer.setAdviserFlagUserName(param.getAdviserFlagUserName());
        customer.setCsFlag(param.getCsFlag());
        customer.setCsFlagUserKey(param.getCsFlagUserKey());
        customer.setCsFlagUserName(param.getCsFlagUserName());
        customer.setPlace(param.getPlace());
        customer.setInitialFollowTime(param.getInitialFollowTime() != null ? new Date(param.getInitialFollowTime()) : null);
        customer.setInitialFollowDesc(param.getInitialFollowDesc());
        customer.setRemark(param.getRemark());
        customer.setEvaluationDate(param.getEvaluationDate() != null ? new Date(param.getEvaluationDate()) : null);
        customer.setVisaExpirationDate(param.getVisaExpirationDate() != null ? new Date(param.getVisaExpirationDate()) : null);
        customer.setAnnexKey(param.getAnnexKey());
        customer.setUpdateUserKey(param.getUpdateUserKey());
        customer.setUpdateUserName(param.getUpdateUserName());
        customer.setUpdateTime(new Date());
        boolean save = sysCustomerService.update(customer, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomer> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomer::getPrimaryKey, param.getCustomerPrimaryKeys());

        SysCustomer customer = new SysCustomer();
        customer.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customer.setUpdateUserKey(param.getUpdateUserKey());
        customer.setUpdateUserName(param.getUpdateUserName());
        customer.setUpdateTime(new Date());
        boolean save = sysCustomerService.update(customer, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
